/* -----------------
* brief
* 1. this is code gen by tools
 */

package middleware

import (
	"github.com/mjiulee/lego"
	"github.com/mjiulee/lego/utils"
	_ "github.com/mjiulee/lego/utils"
	"github.com/valyala/fasthttp"
	"saas_sys_base/common"
)

//type MiddlewareHandle func (next fasthttp.RequestHandler) fasthttp.RequestHandler

var AdminMw = AdminSaasMiddleware //别名，缩写，外部调用可以使用这个短一点的函数名

var AdminSaasMiddleware lego.MiddlewareHandle = func (next fasthttp.RequestHandler) fasthttp.RequestHandler {
	return fasthttp.RequestHandler(func(ctx *fasthttp.RequestCtx) {
		ctxExt := lego.RequestCtxExtent{ctx}
		args := ctx.QueryArgs()

		appid := utils.BytesToInt64(args.Peek("appid"))
		tid := utils.BytesToInt64(args.Peek("tid"))
		who := common.NewWho(tid, appid, 0)

		appidHeader := utils.BytesToInt64(ctxExt.Request.Header.Peek("appid"))
		tidHeader := utils.BytesToInt64(ctxExt.Request.Header.Peek("tid"))
		appidHeader = appidHeader
		tidHeader = tidHeader
		headstring := string(ctxExt.Request.Header.String())
		headstring = headstring
		sessionData := &common.SesionData{
			SysTokenWho: nil,
			AppTokenWho: who,
		}

		common.SetSessionData(&ctxExt, sessionData)

		next(ctx)
		return
		//ctx.UserValue()
		/*
			TOKEN校验逻辑：
			1. token包括Uid、微信小程序对应的mini_app_id
			2. tenent_id\mini_app_id(目前请求所在的微应用的)作为url的参数
			3.将token里的
		*/


	})
}

/*

	var args *fasthttp.Args
	if ctx.IsGet() {
		args = ctx.QueryArgs()
	} else if ctx.IsPost() {
		args = ctx.PostArgs()
	}



	passToken := string(args.Peek("token"))
	timeStamp := string(args.Peek("timestamp"))

	tenantId := utils.BytesToInt64(args.Peek("tid"))
	miniAppId := utils.BytesToInt64(args.Peek("appid"))
	loginmode := utils.BytesToInt(args.Peek("loginmode"))
 */